package com.parleys.androidservice;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.util.Log;
import android.view.MenuItem;

import com.parleys.domain.Channel;
import com.parleys.domain.Presentation;
import com.parleys.helper.ApplicationConstants;

/**
 * Wrapper around a static instance of media player. This way it can be easily used across different activity's
 * 
 * @author David Van Droogenbroeck
 * 
 */
public class ParleysMediaPlayer {

	private static final MediaPlayer MEDIA_PLAYER = new MediaPlayer();

	private static final String TAG = ParleysMediaPlayer.class.getName();

	private static boolean initialized = false;

	public static void setDataSource(String dataSource) {
		try {
			initialized = false;
			MEDIA_PLAYER.reset();
			MEDIA_PLAYER.setDataSource(dataSource);
			MEDIA_PLAYER.prepare();
		} catch (IllegalArgumentException e) {
			Log.e(TAG, "error while setting datasource", e);
		} catch (IllegalStateException e) {
			Log.e(TAG, "error while setting datasource", e);
		} catch (IOException e) {
			Log.e(TAG, "error while setting datasource", e);
		}
	}

	public static void start() {
		try {
			MEDIA_PLAYER.start();
			initialized = true;
		} catch (IllegalStateException e) {
			Log.e(TAG, "error while starting", e);
		}
	}

	public static void moveToPoint(int msecs) {
		try {
			if (MEDIA_PLAYER.isPlaying()) {
				pause();
			}

			MEDIA_PLAYER.seekTo(msecs);
			MEDIA_PLAYER.setOnSeekCompleteListener(new OnSeekCompleteListener() {

				@Override
				public void onSeekComplete(MediaPlayer mp) {
					start();
				}
			});

		} catch (IllegalStateException e) {
			Log.e(TAG, "error while moving to point", e);
		}
	}

	public static void stop() {
		initialized = false;
		MEDIA_PLAYER.stop();
	}

	public static void pause() {
		if (MEDIA_PLAYER.isPlaying()) {
			MEDIA_PLAYER.pause();
		}
	}

	public static void seekTo(int msecs) {
		Log.i(TAG, "Seekto = " + msecs);
		MEDIA_PLAYER.seekTo(msecs);
	}

	public static int getCurrentPosition() {
		return MEDIA_PLAYER.getCurrentPosition();
	}

	public static boolean isPlaying() {
		return MEDIA_PLAYER.isPlaying();
	}

	public static boolean isInitialized() {
		return initialized;
	}

	public static void initializePlayButton(final MenuItem menuItem, final Context context) {
		if (isPlaying()) {
			menuItem.setTitle("Pause").setIcon(android.R.drawable.ic_media_pause);
		} else {
			menuItem.setTitle("Play").setIcon(android.R.drawable.ic_media_play);
		}

	}

	public static void toggle(Channel channel, Presentation presentation, boolean online) {
		if (isPlaying()) {
			pause();
		} else if (isInitialized()) {
			start();
		} else {
			if (online) {
				ParleysMediaPlayer.setDataSource(channel.getMediaBaseURL() + presentation.getId()
						+ presentation.getMp3());
			} else {
				File file = new File(ApplicationConstants.ASSETS_DIRECTORY, presentation.getId()
						+ presentation.getMp3().replace(ApplicationConstants.SLASH, ""));

				ParleysMediaPlayer.setDataSource(file.getAbsolutePath());
			}
			start();
		}
	}
}
